package com.nutiteq.jarmap;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDletStateChangeException;

import com.nutiteq.MapComponent;
import com.nutiteq.components.WgsPoint;
import com.nutiteq.controls.ControlKeys;
import com.nutiteq.listeners.MapListener;
import com.nutiteq.maps.JaredOpenStreetMap;
import com.nutiteq.utils.InitializedCanvas;

public class JarMapCanvas extends Canvas implements InitializedCanvas, MapListener, CommandListener {
  private final JarMapCanvas instance;

  private Image splash;

  private final Timer timer;
  private Thread starter;

  private Command exit;

  private final MapComponent mapComponent;
  private boolean initialized;

  public JarMapCanvas() {
    instance = this;
    try {
      splash = Image.createImage("/splash.png");
    } catch (final IOException e) {
      e.printStackTrace();
    }

    timer = new Timer();
    timer.schedule(new TimerTask() {
      public void run() {
        initialize();
      }
    }, 3000);
    mapComponent = new MapComponent("abcdtrial", JarMap.instance, getWidth(), getHeight(),
        new WgsPoint(24.764580, 59.437420), 5);

  }

  public void initialize() {
    mapComponent.setMap(new JaredOpenStreetMap(256, 3, 3));

    mapComponent.startMapping();
    if (hasPointerEvents()) {
      mapComponent.showDefaultOnScreenZoomControls();
    } else {
      mapComponent.showDefaultCursor();
    }

    mapComponent.defineControlKey(ControlKeys.MOVE_UP_KEY, -1);
    mapComponent.defineControlKey(ControlKeys.MOVE_DOWN_KEY, -2);
    mapComponent.defineControlKey(ControlKeys.MOVE_LEFT_KEY, -3);
    mapComponent.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, -4);
    mapComponent.defineControlKey(ControlKeys.SELECT_KEY, -5);
    //MPowerPlayer
    mapComponent.defineControlKey(ControlKeys.MOVE_UP_KEY, 38);
    mapComponent.defineControlKey(ControlKeys.MOVE_DOWN_KEY, 40);
    mapComponent.defineControlKey(ControlKeys.MOVE_LEFT_KEY, 37);
    mapComponent.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, 39);
    mapComponent.defineControlKey(ControlKeys.SELECT_KEY, 10);
    //for motorola
    mapComponent.defineControlKey(ControlKeys.MOVE_UP_KEY, KEY_NUM2);
    mapComponent.defineControlKey(ControlKeys.MOVE_DOWN_KEY, KEY_NUM8);
    mapComponent.defineControlKey(ControlKeys.MOVE_LEFT_KEY, KEY_NUM4);
    mapComponent.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, KEY_NUM6);
    mapComponent.defineControlKey(ControlKeys.SELECT_KEY, KEY_NUM5);

    mapComponent.setMapListener(this);

    exit = new Command("Exit", Command.EXIT, 0);

    addCommand(exit);
    setCommandListener(this);

    initialized = true;
    repaint();
  }

  protected void paint(final Graphics g) {
    if (!initialized) {
      //paint splash
      g.setColor(0xFFFFFFFF);
      g.setClip(0, 0, getWidth(), getHeight());
      g.fillRect(0, 0, getWidth(), getHeight());
      g.drawImage(splash, (getWidth() - splash.getWidth()) / 2,
          (getHeight() - splash.getHeight()) / 2, Graphics.TOP | Graphics.LEFT);
      return;
    }

    g.setClip(0, 0, getWidth(), getHeight());
    mapComponent.paint(g);
  }

  protected void keyPressed(final int keyCode) {
    if (!initialized) {
      return;
    }
    mapComponent.keyPressed(keyCode);
  }

  protected void keyReleased(final int keyCode) {
    if (!initialized) {
      return;
    }
    mapComponent.keyReleased(keyCode);
  }

  protected void keyRepeated(final int keyCode) {
    if (!initialized) {
      return;
    }
    mapComponent.keyRepeated(keyCode);
  }

  protected void pointerDragged(final int x, final int y) {
    if (!initialized) {
      return;
    }
    mapComponent.pointerDragged(x, y);
  }

  protected void pointerPressed(final int x, final int y) {
    if (!initialized) {
      return;
    }
    mapComponent.pointerPressed(x, y);
  }

  protected void pointerReleased(final int x, final int y) {
    if (!initialized) {
      return;
    }
    mapComponent.pointerReleased(x, y);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == exit) {
      mapComponent.stopMapping();
      try {
        JarMap.instance.destroyApp(true);
      } catch (final MIDletStateChangeException ignore) {
      }
    }
  }

  public void mapClicked(final WgsPoint p) {

  }

  public void mapMoved() {

  }

  public void needRepaint(final boolean mapIsComplete) {
    repaint();
  }
}
